<?php
include_once(LIB_PATH."/InputFilter.php");
class comment_model_post extends Model {
	public function __construct(){
		parent::__construct();
		$this->table = 'app_com_post';
	}
	
	//提交评论
	public function add_post($arguments,$thread_id){
		
		$Filter = new InputFilter($_SERVER['config']['tags'],$_SERVER['config']['tags']);
		
		$arguments = filter_params($arguments,'content','author_id','author_name','type','res_id');
		trim_params($arguments);
		
		$arguments['content']	=	$Filter->process($arguments['content']);
		
		//$arguments['content'] = preg_replace("/<(.*?)>/","",$arguments['content'] );
		//$arguments['content'] = htmlspecialchars($arguments['content']);
		//$arguments['content'] = preg_replace('[/s::/]','',$arguments['content']);
		
		$arguments['create_time'] = time();
		$arguments['thread_id'] = $thread_id;
		$arguments['deleted'] = 0;
		return $this->db->insert($this->table, $arguments);
	}
	
	//读取评论
	public function get_post($data){
		$num = count($data);
		for($i=0;$i<$num;$i++){
			$sql = "select * from ".$this->table." where thread_id=&thread_id and deleted=&deleted order by create_time asc";
			$params = array('thread_id'=>$data[$i]['thread_id'],'deleted'=>'0');
			$result[] = $this->db->fetchALL($sql, $params);
		}
		return $result;
	}
	
	//删除一个主题下的所有评论
	public function delete_comment($thread_id){
		$sql = "select id from ".$this->table." where thread_id=&thread_id and deleted=&deleted";
		$params = array('thread_id'=>$thread_id,'deleted'=>'0');
		$result = $this->db->fetchALL($sql, $params);
		
		$num = count($result);
		for($i=0;$i<$num;$i++){
			//改变deleted的值
			//$sql = "update ".$this->table." set deleted=deleted+1 where id=&id";
			//直接删除
			$sql = "delete  from ".$this->table." where id=&id";
			$params = array('id'=>$result[$i]['id']);
			$this->db->query($sql, $params);
		}
		return $num;
		//删除主题贴相关信息
		//$sql = "delete from app_com_thread where id =". $thread_id;
		//$this->db->query($sql);
	}
	
	//删除一个评论的一个回复
	public function delete_comment_post($uid,$post_id){	
			
			$sql = "delete from " . $this->table . " where id=&id";
			$params = array('id'=>$post_id);
			$this->db->query($sql, $params);			
		
	}
	
	public  function post_new_comment($thread_id){
		$num = count($thread_id);
		for($i=0;$i<$num;$i++){
			 $num2 = count($thread_id[$i]);
			 for($j=0;$j<$num2;$j++){

			 		$sql	= "select * from " . $this->table . " where thread_id=&thread_id and  deleted=&deleted order by create_time asc";
			 		$params = array('thread_id'=>$thread_id[$i][$j]['thread_id'],'deleted'=>0);
			 		$result[$j] = $this->db->fetchAll($sql,$params);

			 }
		}
		return $result;
	}
	
	public function getCountTimeComment($uid,$time){
		
		$sql 	= "select count(*) as num  from " . $this->table . " where author_id =&uid and  create_time >'" . $time . "'";
		$params = array('uid'=>$uid);
		$result = $this->db->fetchRow($sql,$params);
		if($result['num'] == '') $result['num'] =0;
		return $result['num'];
		
	}
	public function getPostDataById($post_id)
	{
		$sql	=	"select * from  " . $this->table . " where id = &id";
		$params =	array('id'=>$post_id);
	    return  	$this->db->fetchRow($sql, $params);
		
	}
	
}